package com.hnhs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hnhs.info.PageInfo;
import com.hnhs.mapper.CategoryMapper;
import com.hnhs.mapper.ProductMapper;
import com.hnhs.model.Category;
import com.hnhs.model.CategoryQuery;
import com.hnhs.model.Product;
import com.hnhs.model.ProductQuery;
import com.hnhs.threadlocalutil.LocalPageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper mapper;

    public List<Category> select(CategoryQuery query){

        if(query.getPageNum()!= null && query.getPageSize() != null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }
        List<Category> selectList = mapper.select(query);
        if(selectList instanceof Page){
            Page page=(Page) selectList;
            Long total= page.getTotal();
            System.out.println(total);
           LocalPageInfoUtil.set(PageInfo.builder().total(total).build());
        }
        return selectList;
    }

    public int insert(Category category){
        return mapper.insert(category);
    }
    public int update(Category category){
        return mapper.update(category);
    }
    public int delete(Integer id){
        return mapper.updateStatus(id,0);
    }
}
